package com.example.jeong.yuejian10.Model;


import com.example.jeong.yuejian10.Bean.PostBean;
import com.example.jeong.yuejian10.Listener.RetrofitListener;
import com.example.jeong.yuejian10.Service.PostService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.example.jeong.yuejian10.constant.Constant.BASE_URL;


public class PostModel {
    private Retrofit retrofit;
    public PostModel(){
        //创建实例
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    //所有帖子列表
    public void getPostlist(final RetrofitListener listener){
        PostService postService = retrofit.create(PostService.class);
        Call call = postService.getAllPostlist();
        Callback<List<PostBean>> callback = new Callback<List<PostBean>>() {
            @Override
            public void onResponse(Call<List<PostBean>> call, Response<List<PostBean>> response) {
                List<PostBean> postBeen = response.body();
                listener.onSuccess(postBeen);
            }
            @Override
            public void onFailure(Call<List<PostBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    //帖子详情
    public  Call<PostBean> getDetail(int p_id,int u_id){
        PostService postService = retrofit.create(PostService.class);
        return postService.getPostDetail(p_id,u_id);
    }


}
